"""
Copyright 2021-2021 The jdh99 Authors. All rights reserved.
ssd1306操作封装.支持多种英文字库
Authors: jdh99 <jdh821@163.com>
"""
import time
_fonts = dict()


def set_font(font, font_size):
    """设置字库"""
    global _fonts
    _fonts[font_size] = font


def display(oled, string, x_axis, y_axis, font_size):
    if font_size not in _fonts:
        return
    if font_size == 12:
        _display_font12(oled, string, x_axis, y_axis)
    if font_size == 16:
        _display_font16(oled, string, x_axis, y_axis)
    if font_size == 24:
        _display_font24(oled, string, x_axis, y_axis)
    if font_size == 32:
        _display_font32(oled, string, x_axis, y_axis)

def is_ascii(ch):
    return len(ch.encode("utf-8")) == 1

def _display_font12(oled, string, x, y):
    offset = 0
    #print(string)
    for k in string:
        #print(k, _fonts[12])
        if k == ' ' or ord(k) == 119:
            x += 12
            continue
        data_code = k.encode("utf-8")
        if is_ascii(k):
            idx = ord(k)
            #print(idx)
            byte_data = _fonts[12][str(idx)]
        else:
            code = 0x00
            #print(data_code)
            code |= data_code[0] << 16   # 第一个字节
            code |= data_code[1] << 8    # 第二个字节
            code |= data_code[2]         # 第三个字节
            # 将三个字节拼成Key得到一个24位的数字
            #print(type(code), code)
            byte_data = _fonts[12][str(code)]
        #print(byte_data)
            
        index = 0
        for y_offset in range(0, 12):  # y_offset  0-16
            a = bin(byte_data[index + y_offset]).replace('0b', '')
            b = bin(byte_data[index + y_offset + 1]).replace('0b', '')
            #print(a,b,index + y_offset,index + y_offset+1)
            index += 1
            
            left8 = "{:0>8}".format(a)
            right8 = "{:0>8}".format(b)
            #print(left8, right8)
            left = left8[2:8]
            right =  right8[4:8] + left8[0:2]
            #print(left,right)
            
            #print("----")
            for x_offset in range(0, 6):  # x_offset 0-8
                pos = 5 - x_offset
                try:
                    oled.pixel(x + x_offset + offset, y + y_offset, int(left[pos]))  # 画前6列
                    #print(x + x_offset, y + y_offset, left[x_ofe)fset])
                    oled.pixel(x + x_offset + offset + 6, y + y_offset, int(right[pos])) #画后6列
                    #print(x + x_offset + 8, y + y_offset, right[x_offset])
                except Exception as e:
                    print(e)
        offset += 12

def _display_font16(oled, string, x, y):
    offset = 0
    #print(string)
    for k in string:
        if k == ' ':
            x += 16
            continue
        data_code = k.encode("utf-8")
        if is_ascii(k):
            idx = ord(k)
            byte_data = _fonts[16][idx]
        else:
            code = 0x00
            #print(data_code)
            code |= data_code[0] << 16   # 第一个字节
            code |= data_code[1] << 8    # 第二个字节
            code |= data_code[2]         # 第三个字节
            # 将三个字节拼成Key得到一个24位的数字
            #print(type(code), code)
            byte_data = _fonts[16][code]
            #print(byte_data)
            
        index = 0
        for y_offset in range(0, 16):  # y_offset  0-16
            a = bin(byte_data[index + y_offset]).replace('0b', '')
            b = bin(byte_data[index + y_offset + 1]).replace('0b', '')
            #print(a,b,index + y_offset,index + y_offset+1)
            index += 1
            
            left = "{:0>8}".format(a)
            right = "{:0>8}".format(b)
            #print(left,right)

            for x_offset in range(0, 8):  # x_offset 0-8
                pos = 7 - x_offset
                oled.pixel(x + x_offset + offset, y + y_offset, int(left[pos]))  # 画前8列
                #print(x + x_offset, y + y_offset, left[x_offset])
                oled.pixel(x + x_offset + offset + 8, y + y_offset, int(right[pos])) #画后8列
                #print(x + x_offset + 8, y + y_offset, right[x_offset])
        offset += 16


def _display_font24(oled, string, x_axis, y_axis):
    offset = 0
    for k in string:
        code = 0x00
        data_code = k.encode("utf-8")
        code |= data_code[0] << 16
        code |= data_code[1] << 8
        code |= data_code[2]
        byte_data = _fonts[24][code]
        for y in range(0, 24):
            a = bin(byte_data[y]).replace('0b', '')
            while len(a) < 8:
                a = '0' + a

            b = bin(byte_data[y + 24]).replace('0b', '')
            while len(b) < 8:
                b = '0' + b

            c = bin(byte_data[y + 48]).replace('0b', '')
            while len(c) < 8:
                c = '0' + c
            for x in range(0, 8):
                oled.pixel(x_axis + offset + x, y + y_axis, int(a[x]))
                oled.pixel(x_axis + offset + x + 8, y + y_axis, int(b[x]))
                oled.pixel(x_axis + offset + x + 16, y + y_axis, int(c[x]))
        offset += 24


def _display_font32(oled, string, x_axis, y_axis):
    offset = 0
    for k in string:
        code = 0x00
        data_code = k.encode("utf-8")
        code |= data_code[0] << 16
        code |= data_code[1] << 8
        code |= data_code[2]
        byte_data = _fonts[32][code]
        for y in range(0, 32):
            a = bin(byte_data[y]).replace('0b', '')
            while len(a) < 8:
                a = '0' + a

            b = bin(byte_data[y + 32]).replace('0b', '')
            while len(b) < 8:
                b = '0' + b

            c = bin(byte_data[y + 64]).replace('0b', '')
            while len(c) < 8:
                c = '0' + c

            d = bin(byte_data[y + 96]).replace('0b', '')
            while len(d) < 8:
                d = '0' + d
            for x in range(0, 8):
                oled.pixel(x_axis + offset + x, y + y_axis, int(a[x]))
                oled.pixel(x_axis + offset + x + 8, y + y_axis, int(b[x]))
                oled.pixel(x_axis + offset + x + 16, y + y_axis, int(c[x]))
                oled.pixel(x_axis + offset + x + 24, y + y_axis, int(d[x]))
        offset += 32


